<template>
  <div class="update-sex">
    <van-picker
      title="性别"
      show-toolbar
      :columns="columns"
      :default-index="value"
      @cancel="$emit('close')"
      @change="onchange"
      @confirm="onconfirm"
    />
  </div>
</template>

<script>
import { updateUserProfile } from '@/api/user'
export default {
  name: 'updateSex',
  props: {
    value: {
      type: Number,
      required: true
    }
  },
  data () {
    return {
      columns: ['男', '女'],
      gender: this.value
    }
  },
  methods: {
    onchange (picker, value, index) {
      this.gender = index
    },
    async onconfirm () {
      this.$toast.loading({
        message: '保存中...',
        forbidClick: true,
        duration: 0
      })
      try {
        await updateUserProfile({
          gender: this.gender
        })
        this.$emit('input', this.gender)
        this.$emit('close')
        this.$toast.success('更新成功')
      } catch (err) {
        this.$toast('操作失败,请重试')
      }
    }
  }
}
</script>

<style scoped lang="less">

</style>
